package com.hzncc.scaffold.controller.Front.Task;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hzncc.scaffold.core.ResultT;
import com.hzncc.scaffold.entity.Other.Exerciserecord;
import com.hzncc.scaffold.entity.Other.Libexerciserecord;
import com.hzncc.scaffold.entity.Other.Taskprogress;
import com.hzncc.scaffold.entity.Vo.UpdateStudyTimeVo;
import com.hzncc.scaffold.mapper.ExerciserecordMapper;
import com.hzncc.scaffold.mapper.LibexerciserecordMapper;
import com.hzncc.scaffold.service.ChapterService;
import com.hzncc.scaffold.service.TaskService;
import com.hzncc.scaffold.service.TaskprogressService;
import com.netflix.eureka.util.batcher.TaskProcessor;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.transform.Result;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: DrunkBear
 * @Date: 2022-06-22 15:30
 * @Description: null
 */
@Api(tags = "前台接口-学习任务")
@RequestMapping("/course/task")
@RestController
public class TaskController {

    @Autowired
    TaskprogressService taskprogressService;

    @Autowired
    TaskService taskService;

    @Autowired
    ChapterService chapterService;

    @Autowired
    LibexerciserecordMapper libexerciserecordMapper;

    @PostMapping("/updateStudyTime")
    @ApiOperation("视频续播")
    public ResultT updateStudyTime(@RequestBody UpdateStudyTimeVo updateStudyTimeVo) {


        LambdaQueryWrapper<Taskprogress> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Taskprogress::getTaskId, updateStudyTimeVo.getTaskId());
        lqw.eq(Taskprogress::getUserId, StpUtil.getLoginIdAsInt());
        lqw.eq(Taskprogress::getCourseId, updateStudyTimeVo.getCourseId());

        Taskprogress taskprogress = taskprogressService.getOne(lqw);

        taskprogress.setStudyTime(updateStudyTimeVo.getStudyTime().toString());

        boolean update = taskprogressService.updateById(taskprogress);

        if (update) {
            return ResultT.success("操作成功");
        } else {
            return ResultT.fail("操作失败");
        }
    }

    @GetMapping("/libExerciseRecordExist")
    @ApiOperation("判断是否有答题记录")
    public ResultT libExerciseRecordExist(Integer taskId) {
        if(!StpUtil.isLogin()){
            ResultT.fail("未登录");
        }

        LambdaQueryWrapper<Libexerciserecord> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Libexerciserecord::getTaskId, taskId);
        lqw.eq(Libexerciserecord::getUserId, StpUtil.getLoginIdAsInt());
        lqw.orderByDesc(Libexerciserecord::getCreateTime);
        lqw.last("limit 1");
        Libexerciserecord libexerciserecord = libexerciserecordMapper.selectOne(lqw);
        Map<String, Integer> map = new HashMap<>();
        if(BeanUtil.isNotEmpty(libexerciserecord)){
            map.put("isExist",1);
        }else {
            map.put("isExist",0);
        }
        return ResultT.success(map);
    }
}
